package com.gk.panda.apis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gk.panda.apis.dto.EquipmentPointDto;
import com.gk.panda.apis.entity.TransectManager.dto.GetEquipmentPointDto;
import com.gk.panda.apis.entity.TransectManager.vo.EquipmentPointVo;
import com.gk.panda.apis.entity.largeScreen.dto.EPListDto;
import com.gk.panda.apis.mapper.EquipmentPointMapper;
import com.gk.panda.apis.service.DeviceManagerService;
import com.gk.panda.apis.service.DictionaryService;
import com.gk.panda.apis.service.EquipmentPointService;
import com.gk.panda.apis.service.OrgService;
import com.gk.panda.pojo.DeviceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentPointServiceImpl implements EquipmentPointService {

    @Autowired
    EquipmentPointMapper equipmentPointMapper;

    @Autowired
    private OrgService orgService;

    @Autowired
    private DeviceManagerService deviceManagerService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 添加设备点位
     * @param equipmentPointDto
     * @return
     */
    @Override
    public int insertequipmentPoint(EquipmentPointDto equipmentPointDto){
        //拼接geoJSon
        String geoJsonStrat = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[[";
        String geoJsonEnd ="]]},\"properties\":{\"ID\":0,\"线路\":0,\"样线编号\":\"待定\",\"县名\":\"待定\"}}";
        String geoJson = geoJsonStrat+ equipmentPointDto.getLongitude()+","+ equipmentPointDto.getLatitude()+geoJsonEnd;
        equipmentPointDto.setGeoJson(geoJson);
        return equipmentPointMapper.insertEquipmentPoint(equipmentPointDto);
    }

    /**
     * 根据id获取设备点位详情
     * @param id
     * @return
     */
    @Override
    public EquipmentPointVo getEPInfo(Integer id) {
        return equipmentPointMapper.getEPInfo(id);
    }

    /**
     * 获取设备点位列表
     * @param equipmentPointDto
     * @return
     */
    @Override
    public List<EquipmentPointVo> getEPList(GetEquipmentPointDto equipmentPointDto) {
//        if (equipmentPointDto.getOrgIds() != null) {
//            List<Integer> orgIds = equipmentPointDto.getOrgIds();
//            List<Integer> list = new ArrayList<>();
//            orgIds.forEach(id -> {
//                List<Integer> ids = orgService.getPuisneOrgIds(id);
//                list.addAll(ids);
//            });
//            List<Integer> collect = list.stream().distinct().collect(Collectors.toList());
//            equipmentPointDto.setOrgIds(collect);
//        }
        return equipmentPointMapper.getEPList(equipmentPointDto);
    }

    /**
     * 根据设备id删除设备点位
     * @param id
     * @return
     */
    @Override
    public int removeEPById(Integer id) {
        return equipmentPointMapper.removeEPById(id);
    }

    /**
     * 修改设备点位
     * @param equipmentPointDto
     * @return
     */
    @Override
    public int updateEquipmentPoint(EquipmentPointDto equipmentPointDto) {
        //拼接geoJSon
        String geoJsonStrat = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[[";
        String geoJsonEnd ="]]},\"properties\":{\"ID\":0,\"线路\":0,\"样线编号\":\"待定\",\"县名\":\"待定\"}}";
        String geoJson = geoJsonStrat+ equipmentPointDto.getLongitude()+","+ equipmentPointDto.getLatitude()+geoJsonEnd;
        equipmentPointDto.setGeoJson(geoJson);
        EquipmentPointVo epInfo = getEPInfo(equipmentPointDto.getUpdateId());
        deviceManagerService.update(Wrappers.<DeviceManager>lambdaUpdate().eq(DeviceManager::getDeviceId,epInfo.getEquipmentId())
                .set(DeviceManager::getLatitude,equipmentPointDto.getLatitude()).set(DeviceManager::getLongitude,equipmentPointDto.getLongitude()));
        return equipmentPointMapper.updateEquipmentPoint(equipmentPointDto);
    }

    /**
     * 同步设备表数据
     * @throws ParseException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void syncDevicePoint() throws ParseException {
        equipmentPointMapper.truncate();
        QueryWrapper<DeviceManager> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag",0).isNotNull("model_type")
                .isNotNull("longitude").isNotNull("latitude");
        List<DeviceManager> devices = deviceManagerService.list(wrapper);
        List<EquipmentPointDto> dtos = new ArrayList<>();
        for (DeviceManager device : devices) {
            EquipmentPointDto dto = new EquipmentPointDto();
            String geoJsonStart = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[[";
            String geoJsonEnd ="]]},\"properties\":{\"ID\":0,\"线路\":0,\"样线编号\":\"待定\",\"县名\":\"待定\"}}";
            String geoJson = geoJsonStart+ device.getLongitude()+","+ device.getLatitude()+geoJsonEnd;
            dto.setEquipmentName(device.getDeviceName());
            dto.setEquipmentType(device.getModelType());
            dto.setGeoJson(geoJson);
            dto.setOrgId(device.getFkOrgId());
            dto.setEquipmentId(device.getDeviceId());
            String newInfraredCameraIcon = dictionaryService.getValue("infrared_camera_icon_new");
            String oldInfraredCameraIcon = dictionaryService.getValue("infrared_camera_icon_old");
            String newVideoCameraIcon = dictionaryService.getValue("video_camera_icon_new");
            String oldVideoCameraIcon = dictionaryService.getValue("video_camera_icon_old");
            String ecologicalEquipmentIcon = dictionaryService.getValue("ecological_equipment_icon");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (device.getModelType().equals("infrared_camera")) {
                Date date = sdf.parse("2022-09-01 00:00:00");
                if (device.getCreateTime().after(date)) {
                    dto.setIcon(newInfraredCameraIcon);
                } else {
                    dto.setIcon(oldInfraredCameraIcon);
                }
            } else if (device.getModelType().equals("video_camera")) {
                Date date = sdf.parse("2022-09-01 00:00:00");
                if (device.getCreateTime().after(date)) {
                    dto.setIcon(newVideoCameraIcon);
                } else {
                    dto.setIcon(oldVideoCameraIcon);
                }
            } else {
                dto.setIcon(ecologicalEquipmentIcon);
            }
            dtos.add(dto);
        }
        equipmentPointMapper.insertEquipmentPointList(dtos);
    }

    /**
     * 大屏获取设备点位
     * @param dto
     * @return
     */
    @Override
    public List<EquipmentPointVo> getEPListForLS(EPListDto dto) {
        return equipmentPointMapper.getEPListForLS(dto);
    }


}
